package pl.wroc.uni.ii.evolution.sampleimplementation;

import pl.wroc.uni.ii.evolution.engine.EvAlgorithm;
import pl.wroc.uni.ii.evolution.engine.individuals.EvBinaryVectorIndividual;
import pl.wroc.uni.ii.evolution.engine.operators.general.composition.EvReplacementComposition;
import pl.wroc.uni.ii.evolution.engine.operators.general.display.EvRealtimeToPrintStreamStatistics;
import pl.wroc.uni.ii.evolution.engine.operators.general.replacement.EvBestFromUnionReplacement;
import pl.wroc.uni.ii.evolution.engine.operators.general.selections.EvTournamentSelection;
import pl.wroc.uni.ii.evolution.engine.operators.spacespecific.binaryvector.EvBinaryVectorBOAOperator;
import pl.wroc.uni.ii.evolution.engine.operators.spacespecific.binaryvector.boa.bayesnetwork.EvBinaryBayesianNetwork;
import pl.wroc.uni.ii.evolution.engine.operators.spacespecific.binaryvector.boa.metrics.EvBayesianDirchletMetric;
import pl.wroc.uni.ii.evolution.engine.operators.spacespecific.binaryvector.boa.strategies.EvGreedyStrategy;
import pl.wroc.uni.ii.evolution.engine.prototype.EvTask;
import pl.wroc.uni.ii.evolution.engine.terminationconditions.EvMaxIteration;
import pl.wroc.uni.ii.evolution.objectivefunctions.EvKPattern;
import pl.wroc.uni.ii.evolution.solutionspaces.EvBinaryVectorSpace;

public class EvBOAExample {

  public static void main(String[] args) {   
    
    EvTask evolutionary_task = new EvTask();
        
    EvAlgorithm<EvBinaryVectorIndividual> genericEA;
   
    EvKPattern objective_function = 
      new EvKPattern(new boolean[] {true, false, true, false}, 4);
   
    genericEA = new EvAlgorithm<EvBinaryVectorIndividual>(2000);
    genericEA.setSolutionSpace(new EvBinaryVectorSpace(objective_function, 40));
   
    genericEA.addOperator(new EvRealtimeToPrintStreamStatistics<EvBinaryVectorIndividual>(System.out));
    
  
    genericEA.addOperator(new EvTournamentSelection<EvBinaryVectorIndividual>(20, 8));
    
    genericEA.addOperator(
        new EvReplacementComposition<EvBinaryVectorIndividual>(
        new EvBinaryVectorBOAOperator<EvBinaryVectorIndividual>(
            new EvBinaryBayesianNetwork(3),
            new EvBayesianDirchletMetric(),
            new EvGreedyStrategy<EvBinaryVectorIndividual>(20),
            4000),
        new EvBestFromUnionReplacement<EvBinaryVectorIndividual>())); 
    
             
    genericEA.setTerminationCondition(new EvMaxIteration<EvBinaryVectorIndividual>(30));
   
    evolutionary_task.setAlgorithm(genericEA);
    long cur_time = System.currentTimeMillis();
    evolutionary_task.run();
    evolutionary_task.printBestResult();
    long after_time = System.currentTimeMillis();
    System.out.println("RUN TIME: " + ((after_time - cur_time)/1000) + " sec");
    System.out.print("best: ");    
    
  }

}
